from unittest.mock import MagicMock, patch

def process_mp4(file_path: str) -> None:
    with open(file_path, "rb") as file_obj:
        data = file_obj.read()
        print(f"Processed data (first 10 bytes): {data[:10]}")

def mock_open_video() -> None:
    """模拟打开一个文件"""
    mock_open = MagicMock()
    mock_file = MagicMock()
    mock_file.read.return_value = b"mock mp4 data"

    # 设置 mock_open 的 __enter__ 方法返回 mock_file
    mock_open.return_value.__enter__.return_value = mock_file
    # 设置 mock_open 的 __exit__ 方法为一个空的 MagicMock 对象
    mock_open.return_value.__exit__ = MagicMock()
    
    # 添加一个side_effect来打印调用信息
    def print_call(*args, **kwargs):
        print(f"open called with args: {args}, kwargs: {kwargs}")
        return mock_open.return_value

    mock_open.side_effect = print_call

    # 使用 patch 来模拟 open 函数
    with patch("__main__.open", mock_open):
        """ 
            with patch("builtins.open", mock_open):
            这种写法也可以,open 函数是内置函数，它被定义在 builtins 模块中。
            相当于全局更换.
        """
        # 调用函数进行测试
        process_mp4("path/to/your/mp4file.mp4")

        # 验证 mock 对象的行为
        mock_open.assert_called_once_with("path/to/your/mp4file.mp4", "rb")
        mock_file.read.assert_called_once()
        mock_open.return_value.__enter__.assert_called_once()
        mock_open.return_value.__exit__.assert_called_once()

# 运行测试
mock_open_video()
